<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/11/29
 * Time: 15:11
 */

namespace app\controllers;


use common\widgets\Tool;

class ReptileController extends BaseController
{
    /**
     * 测试
    **/
    public function actionTest()
    {
        ini_set( 'default_charset', 'GBK' );
        $url = "https://detail.tmall.com/item.htm?id=579157413018&ali_refid=a3_420432_1006:1125091654:N:%E5%A4%96%E5%A5%97:b2afd71b37c8b95afd179fa1774cced8&ali_trackid=1_b2afd71b37c8b95afd179fa1774cced8&spm=a230r.1.14.1&skuId=3846367116434";
        $content = file_get_contents($url);
        preg_match('/<(div)[^c]*class=\"tb-gallery\"[^>]*>.*<\/\\1>/is', $content, $text0);
        //获取标题
//        preg_match('/<(div)[^c]*class=\"tb-booth\"[^>]*>.*<\/\\1>/is', $text0[0], $text1);
        preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $content, $img);
        $url_img = $img[1];
        //获取标题
//        preg_match('/<(div)[^c]*class=\"tb-detail-hd\"[^>]*>.*<\/\\1>/is', $content, $titles);
        preg_match('/<(h1)[^c]*data-spm=\"1000983\"[^>]*>.*<\/\\1>/is', $content, $titles);
        $titles = $titles[0];

        //获取店铺名称
//        preg_match('/<a[^>]*class="slogo-shopname"[^>]*>.*<\/\\1>/is', $content[0], $arr);
        preg_match("/<a.*?class=\".*?slogo-shopname.*?\".*?>(.*?)<\/a>/is", $content, $arr);

        echo $url_img."<br>";
        echo $titles."<br>";
        echo $arr[1];
        exit;


//        preg_match('/<(dl)[^c]*class=\"tm-promo-panel\"[^>]*>.*<\/\\1>/is', $content, $arr);
        preg_match('/<(div)[^c]*class=\"slogo\"[^>]*>.*<\/\\1>/is', $arr[0], $trr);
        print_r($trr[0]);exit;
         echo strip_tags($arr[0]);exit;
//        preg_match('/<(span)[^c]*class=\"tm-price\"[^>]*>.*<\/\\1>/is', $arr[0], $prices);
    }
    /**
     *test1
     **/
    public function actionTest1()
    {
        ini_set( 'default_charset', 'GBK' );
        //测试爬虫 z抓取淘宝数据
//        $url= "https://item.taobao.com/item.htm?spm=a230r.1.14.57.898e72cdK1rL2S&id=531815202400&ns=1&abbucket=20#detail";
//        $url = "https://item.taobao.com/item.htm?spm=a230r.1.14.328.898e72cdK1rL2S&id=576938912602&ns=1&abbucket=20#detail";
        $url = "https://item.taobao.com/item.htm?spm=a230r.1.14.344.898e72cdK1rL2S&id=568133885206&ns=1&abbucket=20#detail";
        $content = file_get_contents($url);
        preg_match('/<(div)[^c]*class=\"tb-detail-bd tb-clear\"[^>]*>.*<\/\\1>/is', $content, $text0);
        //获取标题
        preg_match("/<h3.*?class=\".*?tb-main-title.*?\".*?>(.*?)<\/h3>/is", $content, $titles);
        $title = $titles[1];

        //获取价格
        preg_match("/<em.*?class=\".*?tb-rmb-num.*?\".*?>(.*?)<\/em>/is", $content, $prices);
        $price = $prices[1];

        //图片
//        preg_match('/<div[^>]*class="tb-viewer-contents"[^r]*rc=\"([^"]*)\"[^>]*>/', $content, $img);
        preg_match("/<ul.*?id=\".*?J_UlThumb.*?\".*?>(.*?)<\/ul>/is", $content, $arr);
        preg_match( "/<li.*?class=\".*?tb-selected.*?\".*?>(.*?)<\/li>/is", $arr[1],$imarr);
        preg_match('/<img[^>]*src=[\'"]?([^>\'"\s]*)[\'"]?[^>]*>/i', $imarr[1], $img);
        $img_url = $img[1];

        //抓取店铺信息
        preg_match("/<div.*?class=\".*?tb-shop-name.*?\".*?>(.*?)<\/div>/is", $content, $darr);
        $dpname = strip_tags($darr[1]);
        preg_match("/<a.*?class=\".*?tb-seller-name.*?\".*?>(.*?)<\/a>/is", $content, $warr);
//        print_r($warr);exit;
        $data = [];

        $data['title'] = $title;
        $data['price'] = $price;
        $data['img_url'] = $img_url;
        $data['dname'] = $dpname;
        $data['wwname'] = $warr[1];
        print_r($data);exit;
        return Tool::returnJson($data,200,"成功");

//        preg_match('/<(h3)[^c]*class="tb-main-title"[^>]*>.*<\/\\1>/is', $content, $titles);
//        print_r($titles);exit;
//        $titles = $titles[0];
//        echo $titles;exit;
//
//        preg_match('/<(ul)[^c]*class=\"tb-main-title\"[^>]*>.*<\/\\1>/is', $content, $titles);
//        print_r($titles);exit;
//        $titles = $titles[0];

//        preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $content, $img);
//        $url_img = $img[1];
    }

}